using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Globalization;


namespace OneLine.Components
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class DebugComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteFont debugFont;
        SpriteBatch spriteBatch;
        int frameRate = 0;
        int frameCounter = 0;
        TimeSpan elapsedTime = TimeSpan.Zero;
        ContentManager content;
        CameraComponent currentCamera;
        public DebugComponent(Game game, CameraComponent camera)
            : base(game)
        {
            content = new ContentManager(game.Services);
            currentCamera = camera;
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }


        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            debugFont = Game.Content.Load<SpriteFont>("Fonts/Segoe UI Mono");

            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            content.Unload();
            base.UnloadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            elapsedTime += gameTime.ElapsedGameTime;
            if (elapsedTime > TimeSpan.FromSeconds(1))
            {
                elapsedTime -= TimeSpan.FromSeconds(1);
                frameRate = frameCounter;
                frameCounter = 0;
            }
            // TODO: Add your update code here
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            ++frameCounter;
            
            string fps = string.Format("Frames per second: {0}" +
                                        "\nMouse wheel value: {1}" +
                                        "\nCamera Position: X-'{2}' Y-'{3}' Z-'{3}'" +
                                        "\nCamera Direction: X-'{4}' Y-'{5}' Z-'{6}'"
                                        , 
                                        
                                        frameRate, 
                                        Mouse.GetState().ScrollWheelValue,
                                        currentCamera.Position.X.ToString("0.00000", CultureInfo.InvariantCulture) ,
                                        currentCamera.Position.Y.ToString("0.00000", CultureInfo.InvariantCulture),
                                        currentCamera.Position.Z.ToString("0.00000", CultureInfo.InvariantCulture),
                                        currentCamera.ViewDirection.X.ToString("0.00000", CultureInfo.InvariantCulture),
                                        currentCamera.ViewDirection.Y.ToString("0.00000", CultureInfo.InvariantCulture),
                                        currentCamera.ViewDirection.Z.ToString("0.00000", CultureInfo.InvariantCulture));
            
                                        
            spriteBatch.Begin();
            spriteBatch.DrawString(debugFont, fps, new Vector2(10, 10), Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }



    }
}
